/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.guicommon.dialogs;

import java.io.File;

import com.trolltech.qt.gui.QFileDialog;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QWidget;

public final class CommonDialogs
{
    private static String lastDirectory = System.getenv("user.dir");

    private CommonDialogs()
    {}

    public enum ChangedDialogResult {
        CONTINUE, CANCEL

    };

    public static QMessageBox.StandardButton getFileChangedAction(final QWidget parent)
    {
        final QMessageBox.StandardButtons buttons = new QMessageBox.StandardButtons();
        buttons.set(QMessageBox.StandardButton.Save);
        buttons.set(QMessageBox.StandardButton.Discard);
        buttons.set(QMessageBox.StandardButton.Cancel);

        final String title = parent.tr("Grade Calculator");
        final String message = parent.tr("File has been modified.\n" + "Do you want to save your changes?");

        return QMessageBox.warning(parent, title, message, buttons, QMessageBox.StandardButton.Save);
    }

    public static String getSaveAsFilename(final QWidget parent, final String title, final String filetype,
            final String extension)
    {
        final String filter = parent.tr(filetype) + " (*." + extension + ")";
        final String result = QFileDialog.getSaveFileName(parent, parent.tr(title), lastDirectory,
                new QFileDialog.Filter(filter));

        if ((result == null) || (result.equals(""))) {
            return null;
        }

        final File file = new File(result);
        lastDirectory = file.getParentFile().getAbsolutePath();

        return result;
    }

    public static String getOpenFilename(final QWidget parent, final String title, final String filetype,
            final String extension)
    {
        final String filter = parent.tr(filetype) + " (*." + extension + ")";
        final String result = QFileDialog.getOpenFileName(parent, parent.tr(title), lastDirectory,
                new QFileDialog.Filter(filter));

        if ((result == null) || (result.equals(""))) {
            return null;
        }

        final File file = new File(result);
        lastDirectory = file.getParentFile().getAbsolutePath();

        return result;
    }

}
